package cmd

import (
	"context"
	"net/http"
	_ "net/http/pprof"
	"wxpay/internal/conf"
	"wxpay/pkg/mcontext"
	"wxpay/pkg/protocol/grpc"
	"wxpay/pkg/protocol/rest"

	"go.uber.org/zap"
)

func RunServer() error {
	ctx := context.Background()
	l := mcontext.Logger(ctx)
	err := conf.Init()
	if err != nil {
		l.Panic("init conf failed", zap.Error(err))
	}
	go func() {
		_ = rest.RunServer(ctx, conf.Conf.HTTP.RpcPort, conf.Conf.HTTP.Port)
	}()
	go func() {
		_ = http.ListenAndServe(conf.Conf.HTTP.PProfPort, nil)
	}()
	return grpc.RunServer(ctx, conf.Conf.HTTP.RpcPort, conf.Conf.ORM.DSN)
}
